<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    
    <!-- 操作按钮区域 -->
    <div class="table-operator">

      <!-- 高级查询区域 -->
      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>

    </div>

    <!-- table区域-begin -->
    <div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap"
        :scroll="{x:true}"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
            <a @click="applyLiveSingle(record)">申请入住</a>
            <a-modal v-model="userSingleApplyVisible" title="报名" ok-text="提交申请" cancel-text="取消" @ok="employeeApplySubmit">
                <p v-if="userSingleApplyReminderModel.checkInfoResult && userSingleApplyReminderModel.checkInfoResult.length > 0" style="color: #000">
                    {{userSingleApplyReminderModel.checkInfoResult[0]}}
                </p>
                <p v-if="userSingleApplyReminderModel.checkInfoResult && userSingleApplyReminderModel.checkInfoResult.length > 1" style="color: #6495ED">
                    {{userSingleApplyReminderModel.checkInfoResult[1]}}
                </p>
                <p v-if="userSingleApplyReminderModel.checkInfoResult && userSingleApplyReminderModel.checkInfoResult.length > 2" style="color: #FF7F50">
                    {{userSingleApplyReminderModel.checkInfoResult[2]}}
                </p>
                <p v-if="userSingleApplyReminderModel.checkInfoResult && userSingleApplyReminderModel.checkInfoResult.length > 3" style="color: #FF7F50">
                    {{userSingleApplyReminderModel.checkInfoResult[3]}}
                </p>
                <p v-if="userSingleApplyReminderModel.checkInfoResult && userSingleApplyReminderModel.checkInfoResult.length > 4" style="color: #FF7F50">
                    {{userSingleApplyReminderModel.checkInfoResult[4]}}
                </p>
            </a-modal>
        </span>

      </a-table>
    </div>

  </a-card>
</template>

<script>
    import { filterObj } from '@/utils/util';
    import { deleteAction, getAction, postAction, downFile, getFileAccessHttpUrl } from '@/api/manage'
    import '@/assets/less/TableExpand.less'

  export default {
    name: "needSingleApply",
    components: {
      
    },
    data () {
      return {
        description: '职工报名单身公寓入口',

        queryParam: {},
        dataSource:[],
        ipagination:{
            current: 1,
            pageSize: 10,
            pageSizeOptions: ['10', '20', '30'],
            showTotal: (total, range) => {
            return range[0] + "-" + range[1] + " 共" + total + "条"
            },
            showQuickJumper: true,
            showSizeChanger: true,
            total: 0
        },
        isorter:{
            column: 'createTime',
            order: 'desc',
        },
        filters: {},
        loading:false,
        
        superQueryFlag:false,
        superQueryParams: '',
        superQueryMatchType: 'and',


        userSingleApplyVisible: false,
        userSingleApplyReminderModel: {
            checkInfoResult: [],
            canApply: false,
            applyidx: '',
        },
        
        columns: [
            {
                title: '#',
                dataIndex: '',
                key:'rowIndex',
                width:60,
                align:"center",
                customRender:function (t,r,index) {
                return parseInt(index)+1;
                }
            },
            {
                title:'项目号',
                align:"center",
                dataIndex: 'no'
            },
            {
                title:'项目名',
                align:"center",
                dataIndex: 'name'
            },
            {
                title:'所属省份',
                align:"center",
                dataIndex: 'province'
            },
            {
                title:'所属地市',
                align:"center",
                dataIndex: 'city'
            },
            {
                title:'地址',
                align:"center",
                dataIndex: 'location'
            },
            {
                title:'建设单位',
                align:"center",
                dataIndex: 'buildCompany'
            },
            {
                title:'建成时间',
                align:"center",
                dataIndex: 'buildFinishTime',
                customRender:function (text) {
                return !text?"":(text.length>10?text.substr(0,10):text)
                }
            },
            {
                title:'房屋报建性质',
                align:"center",
                dataIndex: 'type_dictText'
            },
            {
                title:'建筑结构',
                align:"center",
                dataIndex: 'structure'
            },
            {
                title:'楼层数量',
                align:"center",
                dataIndex: 'floorNum'
            },
            {
                title:'梯口数量',
                align:"center",
                dataIndex: 'elevatorNum'
            },
            {
                title:'住房总套数',
                align:"center",
                dataIndex: 'roomNum'
            },
            {
                title:'确权办证时间',
                align:"center",
                dataIndex: 'confirmTime',
                customRender:function (text) {
                return !text?"":(text.length>10?text.substr(0,10):text)
                }
            },
            {
                title: '操作',
                dataIndex: 'action',
                align:"center",
                fixed:"right",
                width:147,
                scopedSlots: { customRender: 'action' },
            }
        ],
        url: {
            list: "/projectroom/znzfProject/listByTypes",
            checkSingleApplyInfo: '/single/znzfNeedSingle/checkapplyinfo',
            employeeSingleApplySubmit: '/single/znzfNeedSingle/insert',

        },
        dictOptions:{},
        superFieldList:[],
      }
    },
    created() {
        this.getSuperFieldList();
        
        this.loadData();
        this.initDictConfig();
    },
    computed: {
        
    },
    methods: {
        initDictConfig(){
        },
        getSuperFieldList(){
            let fieldList=[];
            fieldList.push({type:'string',value:'no',text:'项目号',dictCode:''})
            fieldList.push({type:'string',value:'name',text:'项目名',dictCode:''})
            fieldList.push({type:'string',value:'province',text:'所属省份',dictCode:''})
            fieldList.push({type:'string',value:'city',text:'所属地市',dictCode:''})
            fieldList.push({type:'string',value:'location',text:'地址',dictCode:''})
            fieldList.push({type:'string',value:'buildCompany',text:'建设单位',dictCode:''})
            fieldList.push({type:'date',value:'buildFinishTime',text:'建成时间'})
            fieldList.push({type:'string',value:'type',text:'房屋报建性质',dictCode:"znzf_project_type,name,id"})
            fieldList.push({type:'string',value:'structure',text:'建筑结构',dictCode:''})
            fieldList.push({type:'int',value:'floorNum',text:'楼层数量',dictCode:''})
            fieldList.push({type:'int',value:'elevatorNum',text:'梯口数量',dictCode:''})
            fieldList.push({type:'int',value:'roomNum',text:'住房总套数',dictCode:''})
            fieldList.push({type:'date',value:'confirmTime',text:'确权办证时间'})
            fieldList.push({type:'string',value:'temp1',text:'备用字段1',dictCode:''})
            fieldList.push({type:'string',value:'temp2',text:'备用字段2',dictCode:''})
            this.superFieldList = fieldList
        },
        
        loadData(arg) {
            if(!this.url.list){
                this.$message.error("请设置url.list属性!")
                return
            }
            if (arg === 1) {
                this.ipagination.current = 1;
            }
            var params = this.getQueryParams();//查询条件
            params.filterType = '0';
            this.loading = true;
            getAction(this.url.list, params).then((res) => {
                if (res.success) {
                this.dataSource = res.result.records||res.result;
                if(res.result.total)
                {
                    this.ipagination.total = res.result.total;
                }else{
                    this.ipagination.total = 0;
                }
                }else{
                this.$message.warning(res.message)
                }
            }).finally(() => {
                this.loading = false
            })
        },
        initDictConfig(){
            console.log("--这是一个假的方法!")
        },
        handleSuperQuery(params, matchType) {
            //高级查询方法
            if(!params){
                this.superQueryParams=''
                this.superQueryFlag = false
            }else{
                this.superQueryFlag = true
                this.superQueryParams=JSON.stringify(params)
                this.superQueryMatchType = matchType
            }
            this.loadData(1)
        },
        getQueryParams() {
            //获取查询条件
            let sqp = {}
            if(this.superQueryParams){
                sqp['superQueryParams']=encodeURI(this.superQueryParams)
                sqp['superQueryMatchType'] = this.superQueryMatchType
            }
            var param = Object.assign(sqp, this.queryParam, this.isorter ,this.filters);
            param.field = this.getQueryField();
            param.pageNo = this.ipagination.current;
            param.pageSize = this.ipagination.pageSize;
            return filterObj(param);
        },
        getQueryField() {
            //TODO 字段权限控制
            var str = "id,";
            this.columns.forEach(function (value) {
                str += "," + value.dataIndex;
            });
            return str;
        },

        searchQuery() {
            this.loadData(1);
            this.selectedRowKeys = []
            this.selectionRows = []
        },
        superQuery() {
            this.$refs.superQueryModal.show();
        },
        searchReset() {
            this.queryParam = {}
            this.loadData(1);
        },
        
        handleTableChange(pagination, filters, sorter) {
            if (Object.keys(sorter).length > 0) {
                this.isorter.column = sorter.field;
                this.isorter.order = "ascend" == sorter.order ? "asc" : "desc"
            }
            this.ipagination = pagination;
            this.loadData();
        },

        applyLiveSingle(record){
            if (!this.url.checkSingleApplyInfo) {
            this.$message.error('请设置url.checkSingleApplyInfo属性!')
            return
            }

            if (record.id === '') return
            
            this.userSingleApplyReminderModel = {
                checkInfoResult: [],
                canApply: true,
                applyidx: record.id,
            };

            let params ={
                projectId: record.id,
            }
            postAction(this.url.checkSingleApplyInfo, params).then((res) => {
                if (res.success) {
                    this.userSingleApplyVisible = true;
                    this.userSingleApplyReminderModel.checkInfoResult = res.result.message.split("&");
                    //this.userSingleApplyReminderModel.canApply = res.result.canApply;
                }
                else{
                    this.$message.warning(res.message)
                }
            })
        },
        employeeApplySubmit(){
            if(!this.userSingleApplyReminderModel.canApply){
                this.$message.warning('您当前不符合报名条件！');
                return;
            }
            if(!this.userSingleApplyReminderModel.applyidx){
                this.$message.error('提交错误！')
                return;
            }
            if (!this.url.employeeSingleApplySubmit) {
                this.$message.error('请设置url.employeeSingleApplySubmit!')
                return
            }
            let params ={
                projectId: this.userSingleApplyReminderModel.applyidx,
            }
            postAction(this.url.employeeSingleApplySubmit, params).then((res) => {
                if (res.success) {
                    this.$message.success('申请成功!')
                    this.userSingleApplyVisible = false;
                }
                else{
                    this.$message.warning(res.message);
                }
            })
        },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>